package wiki.cn.edu.tju;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GenMatrix {
	private final String [] keySet = {"sd_era", "sd_name", "sd_authority", "sd_discover_site", "sd_textual", "sd_level"};
	private final boolean [] isVisable = {false, true, true, false, false, false};
	private final String [] keywords0 = {"朝代", "器型", "收藏", "出土", "纹饰", "文物等级"};
	private final String [][] keywords1 = {{"商代", "西周", "春秋", "戰國"}, {"食器", "酒器", "水器", "乐器", "兵器"}, {"中国", "国外"}, {}, {}, {}};
	private final String [][][] keywords2 = {
		{
			{"商（无法分期）", "商中期", "商代晚期", "商代晚期或西周早期"},
			{"西周（无法分期）", "西周早期", "西周中期早段", "西周早中期", "西周中期", "西周中晚期","西周中期或晚期", "西周晚期", "西周中期或春秋早期", "西周晚期或春秋早期"},
			{"春秋（无法分期）", "春秋早期", "春秋中期", "春秋中期或晚期", "春秋中晚期", "春秋晚期", "春秋戰國", "春秋晚期或戰國早期"},
			{"戰國（无法分期）", "戰國早期", "戰國早中期", "戰國中期", "戰國中晚期", "戰國中期或晚期", "戰國晚期"}},
		{
			{"鼎", "鬲", "甗", "簋", "簠", "盨", "敦", "豆"},
			{"爵", "角", "斝", "觚", "觶", "兕觥", "尊", "卣", "盉", "方彝", "勺", "鐘"},
			{"罍", "壺", "盤", "匜", "瓿", "盂", "盉", "鑑"},
			{"鐃", "鐘", "鎛", "鼓座", "錞", "鉦鋮", "鐃", "鐸", "鎛鐘", "甬鐘", "钮钟"},
			{"鉞", "戈", "矛", "戟", "劍", "鈹", "鏃", "盾钖"}},
		{
			{"散居各地", "考古研究所", "首都博物館", "寶雞青銅器博物院", "上海博物館",
				"旅順博物館", "台北故宮博物院", "中國國家博物館", "中國歷史博物館",
				"中國社會科學院考古研究所", "內蒙古", "北京", "南京", "台北", "吉林",
				"四川", "天津", "安徽", "安陽", "山東", "山西", "岐山縣博物館", "廣州",
				"廣東", "廣西", "曲阜市文物管理局", "武功縣文化館", "武漢", "江蘇", "江西",
				"河北", "河南", "洛陽博物館", "浙江", "清華大學圖書館", "湖北", "湖南",
				"滕州博物館", "澳門", "甘肅", "遼寧", "陝西", "香港"},
			{"美國", "丹麥", "倫敦", "加拿大", "德國", "新加坡", "比利時", "日本",
					"法國", "澳大利亞", "瑞典", "瑞士", "義大利", "英國", "荷蘭"}},
		{},
		{},
		{}};
	private static List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		GenMatrix gm = new GenMatrix();
		gm.readData();
		gm.outputJson();
	}

	private void outputJson() {
		// TODO Auto-generated method stub
		int dimAncestor = 0, dimParent = 0, dimLeaf = 0;
		for (int i = 0; i < keywords0.length; i++) {
			if(!isVisable[i]){
				continue;
			}
			dimAncestor ++;
			dimParent += keywords1[i].length;
			for (int j = 0; j < keywords1[i].length; j++) {
				dimLeaf += keywords2[i][j].length;
			}
		}
		System.out.println("dimAncestor = "+dimAncestor+", dimParent = "+dimParent+", dimLeaf = "+dimLeaf+";");
		int[][] matrixAncestor = new int[dimAncestor][dimAncestor];
		int[][] matrixParent = new int[dimParent][dimParent];
		int[][] matrixLeaf = new int[dimLeaf][dimLeaf];
		int indexLeafX = 0, indexLeafY = 0;
		for (int i = 0; i < keywords0.length; i++) {
			if(!isVisable[i]){
				continue;
			}
			for (int j = 0; j < keywords1[i].length; j++) {
				for (int k = 0; k < keywords2[i][j].length; k++) {
					indexLeafX = 0;
					for (int l = 0; l < i; l++) {
						if(isVisable[l]){
							for (int l2 = 0; l2 < keywords1[l].length; l2++) {
								indexLeafX += keywords2[l][l2].length;
							}
						}
					}
					for (int l = 0; l < j; l++) {
						indexLeafX += keywords2[i][l].length;
					}
					indexLeafX += k;
					for (int i2 = 0; i2 < keywords0.length; i2++) {
						if(!isVisable[i2]){
							continue;
						}
						
						for (int j2 = 0; j2 < keywords1[i2].length; j2++) {
							for (int k2 = 0; k2 < keywords2[i2][j2].length; k2++) {
								indexLeafY = 0;
								for (int l = 0; l < i2; l++) {
									if(isVisable[l]){
										for (int l2 = 0; l2 < keywords1[l].length; l2++) {
											indexLeafY += keywords2[l][l2].length;
										}
									}
								}
								for (int l = 0; l < j2; l++) {
									indexLeafY += keywords2[i2][l].length;
								}
								indexLeafY += k2;
								
//								System.out.println(indexLeafX+"--"+indexLeafY);
								if(i != i2 ){
									matrixLeaf[indexLeafX][indexLeafY] = getJoint(keySet[i], keywords2[i][j][k], keySet[i2], keywords2[i2][j2][k2]);
								}else{
									matrixLeaf[indexLeafX][indexLeafY] = 0;
								}
							}
						}
					}
				}
			}
		}
		
		//outputMatrix
		String resultFile = "var matrixLeaf = [\n";
		for (int i = 0; i < matrixLeaf.length; i++) {
			resultFile += "[";
			for (int j = 0; j < matrixLeaf.length; j++) {
				resultFile += (j==matrixLeaf.length-1?matrixLeaf[i][j]:matrixLeaf[i][j] + ",");
			}
			resultFile += (i==matrixLeaf.length-1?"]\n":"],\n");
		}
		resultFile += "];";
		System.out.println(resultFile);
		
		int rowIndex = 0, parIndex = 0;
		for (int i = 0; i < keywords0.length; i++) {
			if(!isVisable[i]){
				continue;
			}
			for (int j = 0; j < keywords1[i].length; j++) {
				for (int j2 = 0; j2 < keywords2[i][j].length; j2++) {
					for (int l = 0; l < matrixLeaf.length; l++) {
						matrixParent[parIndex + j][parIndex + j] += matrixLeaf[rowIndex+j2][l];
					}
				}
				rowIndex += keywords2[i][j].length;
			}
			parIndex += keywords1[i].length;
		}
		
		String resultFileParent = "var matrixParent = [\n";
		for (int i = 0; i < matrixParent.length; i++) {
			resultFileParent += "[";
			for (int j = 0; j < matrixParent.length; j++) {
				resultFileParent += (j==matrixParent.length-1?matrixParent[i][j]:matrixParent[i][j] + ",");
			}
			resultFileParent += (i==matrixParent.length-1?"]\n":"],\n");
		}
		resultFileParent += "];";
		System.out.println(resultFileParent);
		
		int ansIndex = 0, ancIndex = 0;
		for (int i = 0; i < keywords0.length; i++) {
			if(!isVisable[i]){
				continue;
			}
			for (int j = 0; j < keywords1[i].length; j++) {
				for (int j2 = 0; j2 < matrixParent.length; j2++) {
					matrixAncestor[ancIndex][ancIndex] += matrixParent[ansIndex + j][j2];
				}
			}
			ansIndex += keywords1[i].length;
			ancIndex ++;
		}
		
		String resultFileAncestor = "var matrixAncestor = [\n";
		for (int i = 0; i < matrixAncestor.length; i++) {
			resultFileAncestor += "[";
			for (int j = 0; j < matrixAncestor.length; j++) {
				resultFileAncestor += (j==matrixAncestor.length-1?matrixAncestor[i][j]:matrixAncestor[i][j] + ",");
			}
			resultFileAncestor += (i==matrixAncestor.length-1?"]\n":"],\n");
		}
		resultFileAncestor += "];";
		System.out.println(resultFileAncestor);
		
		String leafTextLst = "var labelList = [";
		for (int i = 0; i < keywords0.length; i++) {
			if(!isVisable[i]){
				continue;
			}
			for (int j = 0; j < keywords1[i].length; j++) {
				for (int j2 = 0; j2 < keywords2[i][j].length; j2++) {
					leafTextLst += ("\n{title: \"" + keywords2[i][j][j2] + "\"},");
				}
			}
		}
		leafTextLst = leafTextLst.substring(0, leafTextLst.length()-1) + "\n];";
		System.out.println(leafTextLst);
	}

	private void readData() throws Exception {
		String qryStr = "select * from sinica_data";
		String schema = "my_wikidb";
		String[] keySet = { "sd_no", "sd_name", "sd_era", "sd_discover_time",
				"sd_discover_site", "sd_discover_adrr", "sd_authority",
				"sd_transfer", "sd_size", "sd_textual", "sd_imgUrl",
				"sd_content_explain", "sd_content_imgUrl" };
		dataList = DataCon.getDataList(schema, qryStr, keySet);
		for (int i = 0; i < dataList.size(); i++) {
			for (int j = 0; j < keySet.length; j++) {
				dataList.get(i).put(
						keySet[j],
						dataList.get(i).get(keySet[j]).toString()
								.replaceAll("&nbsp;", "").replaceAll("\\", "")
								.replaceAll("\\", "").replaceAll("\n", "")
								.replaceAll("                ", "").replaceAll("            ", ""));
			}
			Map<String, Object> itemMap = dataList.get(i);
			itemMap.put("sd_discover_site", 
					itemMap.get("sd_discover_site").toString().contains("<") ? 
					itemMap.get("sd_discover_site").toString()
					.substring(0,itemMap.get("sd_discover_site").toString().indexOf("<"))
					: itemMap.get("sd_discover_site").toString());
			itemMap.put("sd_authority",
					itemMap.get("sd_authority").toString().contains("<") ? 
					itemMap.get("sd_authority").toString()
					.substring(0,itemMap.get("sd_authority").toString().indexOf("<"))
					: itemMap.get("sd_authority").toString());
			dataList.set(i, itemMap);
		}
	}
	
	private static int getJoint(String key1, String str1, String key2, String str2){
		int jointNum = 0;
		for (int i = 0; i < dataList.size(); i++) {
			if(dataList.get(i).get(key1).toString().contains(str1) && dataList.get(i).get(key2).toString().contains(str2)){
				jointNum ++;
			}
		}
		return jointNum;		
	}
}
